cGeoController = new MyClass({
	geoCoding 			: null, //0: no implementa, 1: google, 2: bykom, 3: cloudmade
	reverseGeoCoding	: null, //0: no implementa, 1: google, 2: bykom, 3: cloudmade
	routing				: null, //0: no implementa, 1: google, 2: bykom, 3: cloudmade
	
	init : function(geo, reverseGeo, routing){
		this.geoCoding			= geo.toString();
		this.reverseGeoCoding 	= reverseGeo.toString();
		this.routing 			= routing.toString();
	},
	implementaGeoCoding: function(){
		if(this.geoCoding != "0"){			
			return true;
		}else{
			return false;
		}
	},
	implementaReverseGeoCoding: function(){
		if(this.reverseGeoCoding != "0"){			
			return true;
		}else{
			return false;
		}
	},
	implementaRouting: function(){
		if(this.routing != "0"){			
			return true;
		}else{
			return false;
		}
	},
	/******************************
	 ***********GOOGLE*************
	 *****************************/
	implementaGeoGoogle: function(){
		if(this.geoCoding!=null){
			if(this.geoCoding == "1"){			
				return true;
			}else{
				return false;
			}
		}else{
			return false;
		}
	},
	implementaReverseGeoGoogle: function(){
		if(this.reverseGeoCoding!=null){
			if(this.reverseGeoCoding == "1"){			
				return true;
			}else{
				return false;
			}
		}else{
			return false;
		}
	},
	
	implementaRoutingGoogle: function(){
		if(this.routing!=null){
			if(this.routing == "1"){			
				return true;
			}else{
				return false;
			}
		}else{
			return false;
		}
	},
	/******************************
	 ***********BYKOM*************
	 *****************************/
	implementaGeoBykom: function(){
		if(this.geoCoding!=null){
			if(this.geoCoding == "2"){			
				return true;
			}else{
				return false;
			}
		}else{
			return false;
		}
	},
	implementaReverseGeoBykom: function(){
		if(this.reverseGeoCoding!=null){
			if(this.reverseGeoCoding == "2"){			
				return true;
			}else{
				return false;
			}
		}else{
			return false;
		}
	},
	
	implementaRoutingBykom: function(){
		if(this.routing!=null){
			if(this.routing == "2"){			
				return true;
			}else{
				return false;
			}
		}else{
			return false;
		}
	},
	/******************************
	 ***********CLOUDMADE*************
	 *****************************/
	implementaGeoCloudMade: function(){
		if(this.geoCoding!=null){
			if(this.geoCoding == "3"){			
				return true;
			}else{
				return false;
			}
		}else{
			return false;
		}
	},
	implementaReverseGeoCloudMade: function(){
		if(this.reverseGeoCoding!=null){
			if(this.reverseGeoCoding == "3"){			
				return true;
			}else{
				return false;
			}
		}else{
			return false;
		}
	},
	
	implementaRoutingCloudMade: function(){
		if(this.routing!=null){
			if(this.routing == "3"){			
				return true;
			}else{
				return false;
			}
		}else{
			return false;
		}
	},
	
	getStatus: function(status){
		var error = "";
		switch(status){
			case google.maps.GeocoderStatus.ZERO_RESULTS: error = "No hay resultados para la consulta realizada.";
				break;
			case google.maps.GeocoderStatus.INVALID_REQUEST: error = "Consulta invalida.";
				break;
			case google.maps.GeocoderStatus.OVER_QUERY_LIMIT: error = "La p�gina web ha superado el l�mite de solicitudes en un per�odo de tiempo demasiado breve.";
				break;
			case google.maps.GeocoderStatus.REQUEST_DENIED: error = "No se permite que la p�gina web utilice el geocoder.";
				break;
			case google.maps.GeocoderStatus.UNKNOWN_ERROR: error = "No se pudo procesar una solicitud de codificaci�n geogr�fica debido a un error del servidor. Puede que la solicitud se realice correctamente si lo intentas de nuevo.";
				break;
			case google.maps.GeocoderStatus.ERROR: error = "Se ha producido un error al establecer la comunicaci�n con los servidores de Google.";
				break;
			default: error = "desconocido";
				break;
		}
		return error;
	},
	getTravelMode: function(modoViaje){
		var modoDeViaje = "";
		switch(modoViaje){//seteo el modo de viaje
			case 1: modoDeViaje = google.maps.DirectionsTravelMode.DRIVING;
				break;
			case 2: modoDeViaje = google.maps.DirectionsTravelMode.WALKING;
				break;
			case 3: modoDeViaje = google.maps.DirectionsTravelMode.BICYCLING;
				break;
			default: modoDeViaje = google.maps.DirectionsTravelMode.DRIVING;
				break;
		}
		return modoDeViaje;
	},
	/**
	 * Devuelve el punto a partir de una direccion<<String>>.
	 */
	getPointFromAddress: function(address,divId){
		if(this.implementaGeoCoding()){
			if(this.implementaGeoGoogle()){
				var geocoder = new google.maps.Geocoder();
				geocoder.geocode( { 'address': address}, function(results, status) {
				      if (status == google.maps.GeocoderStatus.OK) {
				    	  var res = results[0].geometry.location;
				    	  var p = new Array();
				    	  p[0] = res.lng();
				    	  p[1] = res.lat();
				    	  if(divId==null){
				    		  pointFromAddressOk(p);
				    	  }else{
				    		  window.parent.frames[divId].pointFromAddressOk(p);
				    	  }
				      } else {
				    	  var error = cGeoController.getStatus(status);
				    	  var msg = "Error = '" + error + "'<br>"; 
				    	  if(divId==null){
				    		  pointFromAddressError(msg);
				    	  }else{
				    		  window.parent.frames[divId].pointFromAddressError(msg);
				    	  }
				      }
				 });
			}else if(this.implementaGeoCloudMade()){
				alert(address);
//				var geocoder = new CM.Geocoder(CLOUDMADE_KEY);
//				geocoder.getLocations(address, function(response) {
//					for (var i = 0; i < response.features.length; i++) {
//						var coords = response.features[i].centroid.coordinates;
//					}
//					var p = new Array();
//					//lon
//			    	p[0] = response.features[0].centroid.coordinates[1];
//			    	//lat
//			    	p[1] = response.features[0].centroid.coordinates[0];
//			    	window.parent.frames[divId].pointFromAddressOk(p);
//				});
				
//				var url = "http://geocoding.cloudmade.com/"+CLOUDMADE_KEY+"/geocoding/v2/find.js?query="+address+"";
//				$j.ajax({
//					  url: url,
//				      data: "",
//				      beforeSend: function(){
//						//$j('#LoaderAjax').show();
//				      },
//				      success: function(data) {
//				    	//$j('#LoaderAjax').hide();
//				    	  alert(data);
//				    	if(data.trim()!=""){
//					        var resultsHistorico =	jQuery.parseJSON(data);
//				    	}
//				      }
//				});
//				GeocodeCallback = function(result){
//					alert("Hola");
//				}
//
//				var url = "http://where.yahooapis.com/geocode?appid="+YAHOOMAP_KEY+"&"+address+"&flags=J";
//				$j.ajax({
//					  url: url,
//				      data: "", 
//				      dataType: "json", 
//				      beforeSend: function(){
//						//$j('#LoaderAjax').show();
//				      },
//				      success: function(data) {
//				    	  alert(jQuery.parseJSON(data));
//				    	
//				      },
//				      complete: function(data){
//				    	  alert("complete");
//				    	  alert(jQuery.parseJSON(data));
//				      }
//				});
				
				/*
				GeocodeCallback = function(result){
					if(result.resourceSets[0].resources.length >0){
						var p = new Array();
						//lon
						p[1] = result.resourceSets[0].resources[0].point.coordinates[0];
						//lat
						p[0] = result.resourceSets[0].resources[0].point.coordinates[1];
						window.parent.frames[divId].pointFromAddressOk(p);
						alert("Found location: " + result.resourceSets[0].resources[0].name);
					}
				}
				
				CallRestService = function(request){
					var script = document.createElement("script");
					script.setAttribute("type", "text/javascript");
					script.setAttribute("src", request);
					document.body.appendChild(script);
				}
				var url = "http://dev.virtualearth.net/REST/v1/Locations?output=json&jsonp=GeocodeCallback&"+address+"&key="+MSMAP_KEY+"";
				CallRestService(url);
				*/
				
				
			}else if(this.implementaGeoBykom()){
				var SERVERURL 		= LOGIN.SERVER_URL;
				var SERVERPORT 		= LOGIN.SERVER_PORT;
				var SERVERCONTEXT 	= LOGIN.SERVER_CONTEXT;
				var url = "http://"+SERVERURL+":"+SERVERPORT+"/"+SERVERCONTEXT+"/geocoder?q="+address+"&output=jsonp";
				//var url = "http://192.168.2.83:8501/geocoder?q="+address+"&output=jsonp";

				GeocodeCallback = function(result){//200,10,lon,lat
					var vResp = ""; 
					if(result!=""){
						vResp = result.split(",");
						if(vResp[0].toString() == "200"){
							var p = new Array();
							//lon
							p[1] = parseFloat(vResp[3]);
							//lat
							p[0] = parseFloat(vResp[2]);
							if(divId==null){
					    		  pointFromAddressOk(p);
							}else{
				    		  window.parent.frames[divId].pointFromAddressOk(p);
							}
						}else{
							var error = "No se encontraron datos";
				    	  	var msg = "Error = '" + error + "'<br>"; 
				    	 	if(divId==null){
				    	 		pointFromAddressError(msg);
				    	  	}else{
				    	  		window.parent.frames[divId].pointFromAddressError(msg);
				    	  	}
						}
					}
				}
				CallRestService = function(request){
					var script = document.createElement("script");
					script.setAttribute("type", "text/javascript");
					script.setAttribute("src", request);
					document.body.appendChild(script);
				}
				var url = url+"&callback=GeocodeCallback";
				CallRestService(url);
				
			}
		}
	},
	/**
	 * Devuelve la direccion a partir de un <<OpenLayers.LonLat>>(lat,long).
	 */
	getAddressFromLonLat: function(lonlat,divId){
		if(this.reverseGeoCoding()){
			if(this.reverseGeoCoding()){
				var latlng = new google.maps.LatLng(lonlat.lat, lonlat.lon);
				var geocoder = new google.maps.Geocoder();
				geocoder.geocode( { 'latLng': latlng}, function(results, status) {
				      if (status == google.maps.GeocoderStatus.OK) {
				    	  var sAddress = "";
				    	  for(var i=0;i<results[0].address_components.length;i++){
				    		  sAddress += results[0].address_components[i].long_name +"\n";
				    	  }
				    	  //window.parent.frames[divId].pointFromLonLatOk(sAddress);
				      } else {
				    	  var error = cGeoController.getStatus(status);
				    	  var msg = "Error = '" + error + "'<br>";
				    	 //window.parent.frames[divId].pointFromLonLatError(msg);
				      }
				 });
			}
		}
	},
	getRoute: function(request,oParams){
		if(this.implementaRouting()){
			if(this.implementaRoutingGoogle()){
				$j("#LoaderAjaxRutas").show();
//				if (GBrowserIsCompatible()){
			        gdir = new google.maps.DirectionsService();
			        request.travelMode 	= this.getTravelMode(oParams.modoViaje);
			        if(request.origin==null && request.destination==null){//si viene sin origen y destino los seteo con los que se pasan en el objeto oParams
				        var oLatLongOrig 	= new google.maps.LatLng(oParams.movilLat,oParams.movilLon);
						var oLatLongDest 	= new google.maps.LatLng(oParams.ptoLat,oParams.ptoLon);
				        request.origin 		= oLatLongOrig;
				        request.destination = oLatLongDest;
			        }
			        gdir.route(request, function(response, status){
			        	var dist="";
			            if(status == google.maps.DirectionsStatus.OK){
			            	var myRoute 		= response.routes[0];
			            	var myRouteBounds 	= myRoute.bounds;
			                var myRouteLegs 	= myRoute.legs[0];
			                var myRouteSteps 	= myRoute.legs[0].steps;
			                dist 				= myRouteLegs.distance.value;
			                distFormateado 		= myRouteLegs.distance.text;
			                steps				= myRouteSteps;
			                resp 				= response;
			            }
			            $j("#LoaderAjaxRutas").hide();
			            /////RESPUESTA PARA MISPUNTOS/////
			            if(oParams.peticion.toString().toLowerCase() == "mispuntos"){
			            	var movilId = oParams.vehiculosVisiblesId[oParams.i].toString();
				            if(dist){
				            	var distMovil 	= new Object();
				            	distMovil.movilId 	= movilId;
				            	distMovil.distance 	= dist;
				            	distMovil.distanceText = distFormateado;
				            	distMovil.steps = steps;
				            	distMovil.legs  = myRouteLegs;
				            	distMovil.resp  = resp;
				            	oParams.distances.push(distMovil);
				            }
				            oParams.gDC.distanceDWBToMovil(oParams.gDC,oParams.gD,oParams.vehiculosVisiblesId,oParams.cant-1,oParams.i+1,oParams.distances,oParams.idCercanos,oParams.modoViaje);
				        /////RESPUESTA PARA MISRUTAS/////
			            }else if(oParams.peticion.toString().toLowerCase() == "misrutas"){
				            if(dist){
				            	var ruta 	= new Object();
				            	ruta.distance 	= dist;
				            	ruta.distanceText = distFormateado;
				            	ruta.steps = steps;
				            	ruta.legs  = myRouteLegs;
				            	var directionsDisplay = new google.maps.DirectionsRenderer();
				            	directionsDisplay.setPanel(document.getElementById("directivas"));
				            	directionsDisplay.setDirections(resp);
				            	cMapController.getMapActive().dibujarRutaPorDireccion(ruta);
				            	$j( "#dialog-form" ).css("height","450px");
				            }else{
				            	cMapController.getMapActive().setEstadoNuevaRuta();
				            	alert("No se encontroraron rutas con los datos especificados.");
				            }
			            }
			        });
//				}
			}else if(this.implementaRoutingBykom()){
				alert("No tiene este servicio activado. Consulte a su administrador.");
			}
		}
	}	
});